/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormAlarmRing.cpp
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "forms/FormAlarmRing.h"
#include "SCFormManager.h"
#include "SCNavigator.h"
#include "models/SCHelpers.h"
#include "models/SCSession.h"
#include "SCDatabase.h"
#include "SCTranslator.h"

FormAlarmRing::FormAlarmRing()
{
}

FormAlarmRing::~FormAlarmRing()
{
}

bool
FormAlarmRing::Initialize(void)
{
	Construct(L"IDF_ALARM_RING");
	return true;
}

result
FormAlarmRing::OnInitializing(void)
{
	this->pTime = static_cast<Label*>(GetControl("IDC_LABEL_TIME"));

	//header
	this->pHeader = this->GetHeader();
	pHeader->SetStyle(HEADER_STYLE_SEGMENTED);
	pHeader->AddActionEventListener(*this);

	//MarkOne
	HeaderItem* pMarkOne = new HeaderItem();
	pMarkOne->Construct(IDA_MARK_ONE);
	pMarkOne->SetText("1");

	//MarkTwo
	HeaderItem* pMarkTwo = new HeaderItem();
	pMarkTwo->Construct(IDA_MARK_TWO);
	pMarkTwo->SetText("2");

	//MarkThree
	HeaderItem* pMarkThree = new HeaderItem();
	pMarkThree->Construct(IDA_MARK_THREE);
	pMarkThree->SetText("3");

	pHeader->AddItem(*pMarkOne);
	pHeader->AddItem(*pMarkTwo);
	pHeader->AddItem(*pMarkThree);

	//footer
	Footer* pFooter = this->GetFooter();
	FooterItem* pItemConfirm = new FooterItem();
	pItemConfirm->Construct(IDA_ALARM_RING_CONFIRM);
	pItemConfirm->SetText(SCTranslator::GetText("IDS_ALARMS_RING"));
	pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);
	pFooter->AddItem(*pItemConfirm);
	pFooter->AddActionEventListener(*this);

	delete pItemConfirm;
	delete pMarkOne;
	delete pMarkTwo;
	delete pMarkThree;

	return E_SUCCESS;
}

result
FormAlarmRing::OnTerminating(void)
{

	return E_SUCCESS;
}

void
FormAlarmRing::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch (actionId)
	{
		case IDA_ALARM_RING_CONFIRM:
			this->StopWakeUp();
			SCFormManager::SwitchToForm(SCFormManager::IDF_ALARM, false, null, SCFormManager::NO_SLIDE);
			break;
		case IDA_MARK_ONE:
			pLastSession->SetQualityOfSleep(1);
			break;
		case IDA_MARK_TWO:
			pLastSession->SetQualityOfSleep(2);
			break;
		case IDA_MARK_THREE:
			pLastSession->SetQualityOfSleep(3);
			break;
		case IDA_MARK_FOUR:
			pLastSession->SetQualityOfSleep(4);
			break;
	}
}

void
FormAlarmRing::OnPlayerBuffering (int percent)
{
}

void
FormAlarmRing::OnPlayerEndOfClip (void)
{
}

void
FormAlarmRing::OnPlayerErrorOccurred (PlayerErrorReason r)
{
}

void
FormAlarmRing::OnPlayerInterrupted (void)
{
}

void
FormAlarmRing::OnPlayerOpened (result r)
{
	this->pPlayer->Play();
}

void
FormAlarmRing::OnPlayerReleased (void)
{
}

void
FormAlarmRing::OnPlayerSeekCompleted (result r)
{
}

void
FormAlarmRing::SetAlarm(SCAlarm* pAlarm)
{
	this->pAlarm = pAlarm;
}

SCAlarm*
FormAlarmRing::GetAlarm()
{
	return this->pAlarm;
}

int
FormAlarmRing::GetFormId()
{
	return SCFormManager::IDF_ALARM_RING;
}

void
FormAlarmRing::StartWakeUp()
{
	PowerManager::KeepScreenOnState(true, false);

	this->pLastSession = SCSession::GetLastSession();
	this->pLastSession->SetQualityOfSleep(2);

	//setActiveButton
	this->pHeader->SetItemSelected(1);

	this->StartAwakeType();
	this->StartAwakeTask();
}

void
FormAlarmRing::StopWakeUp()
{
	if(this->pPlayer != NULL)
	{
		this->pPlayer->Stop();
		delete this->pPlayer;
	}

	if(this->pVibrator != NULL)
	{
		//this->pVibrator->Stop();
		delete this->pVibrator;
	}

	//Save Session
	this->pLastSession->Save();

	// deactive alarm - OnlyOnce
	if(this->pAlarm->GetAlarmRepeat()->IsJustOnlyOnce())
	{
		this->pAlarm->SetActive(false);
		this->pAlarm->Save();

		//cache data
		int index;
		ArrayList *pAlarms = SCAlarm::GetAllAlarms();
		if(pAlarms->IndexOf(*this->pAlarm, index) == E_SUCCESS)
		{
			SCDatabase::AddDataModification(SCDatabase::ACTION_MODIFY, SCFormManager::IDF_ALARM, index);
			SCDatabase::SetReloadAlarms(true);
		} else {
			AppLog("Not found.");
		}
	}

	PowerManager::KeepScreenOnState(false, false);
}

void
FormAlarmRing::StartAwakeType()
{
	switch(this->pAlarm->GetAwakeType())
	{
		case AWAKE_TYPE_SOUND:
			this->StartAlarmPlayer();
			break;
		case AWAKE_TYPE_SOUND_VIBRATION:
			this->StartAlarmPlayer();
			this->StartVibration();
			break;
		case AWAKE_TYPE_VIBRATION:
			this->StartVibration();
			break;
	}
}

void
FormAlarmRing::StartAwakeTask()
{

}

void
FormAlarmRing::StartVibration()
{
	this->pVibrator = new Vibrator();
	this->pVibrator->Construct();
	this->pVibrator->Start(2000, 1000, 50, 100);
}

void
FormAlarmRing::StartAlarmPlayer()
{
	String basePath("/Home/alarm_sounds/");
	basePath.Append(pAlarm->GetAwakeSound());

	//SetVolume???

	//Set Player
	this->pPlayer = new Player();
	this->pPlayer->Construct(*this, 0);
	this->pPlayer->OpenFile(basePath, true);
	this->pPlayer->SetLooping(true);
	this->pPlayer->SetVolume(this->pAlarm->GetSoundLevel()*10);
}

void
FormAlarmRing::Update()
{
	if(this->pAlarm != NULL)
	{
		this->pTime->SetText(SCHelpers::ConvertDateTimeToString(this->pAlarm->GetTime(), "HH:mm"));
	}
}

void
FormAlarmRing::Redraw()
{
	this->Update();
	RequestRedraw(true);
}
